
package es.ucm.disia.xsdp.wizard;

import java.awt.Dimension;

import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;

public class TreeGUI
{
	private JScrollPane treeView;
	private DefaultMutableTreeNode top;
	private JTree tree;

	public TreeGUI(DefaultMutableTreeNode raiz) 
	{
		top = raiz;
		tree = new JTree(top);
		tree.getSelectionModel().setSelectionMode(
				TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);

		// Listen for when the selection changes.
		tree.addTreeSelectionListener(new TreeSelectionListener() {
			public void valueChanged(TreeSelectionEvent e)
			{
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) (e
						.getPath().getLastPathComponent());
				Object nodeInfo = node.getUserObject();
			}
		});

		treeView = new JScrollPane(tree);
		treeView.setMinimumSize(new Dimension(200, 450));
		treeView.setPreferredSize(new Dimension(360, 280));
		treeView.createVerticalScrollBar();
	}

	public void setArbol(DefaultMutableTreeNode raiz)
	{
		top.removeAllChildren();
		top = raiz;
	}

	public JComponent gui()
	{
		tree = new JTree(top);

		// Expandir arbol
		for (int i = 0; i < tree.getRowCount(); i++)
		{
			tree.expandRow(i);
		}

		treeView = new JScrollPane(tree);
		return treeView;
	}

	public void clear()
	{
		top.removeAllChildren();
		tree.removeAll();
		tree.clearSelection();
	}
}